package net.gyapp.markim.module.message.adapter;

import android.text.style.ImageSpan;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.easyder.wrapper.utils.UIUtils;
import com.lqr.emoji.MoonUtils;

import net.gyapp.markim.R;
import net.gyapp.markim.core.ImHelper;
import net.gyapp.markim.core.reponse.ChatResponse;
import net.gyapp.markim.core.reponse.ImResponse;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;
import net.gyapp.markim.module.message.bean.VoiceBean;

import org.jim.common.packets.ChatType;
import org.jim.common.packets.MsgType;

import java.util.List;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/7/18.
 * Desc:
 */

public class ChatAdapter extends BaseQuickAdapter<ImResponse, BaseRecyclerHolder> {

    private static final int MSG_SEND_TEXT = R.layout.item_chat_text_send;
    private static final int MSG_RECEIVE_TEXT = R.layout.item_chat_text_receive;
    private static final int MSG_SEND_IMAGE = R.layout.item_chat_image_send;
    private static final int MSG_RECEIVE_IMAGE = R.layout.item_chat_image_receive;
    private static final int MSG_SEND_AUDIO = R.layout.item_chat_audio_send;
    private static final int MSG_RECEIVE_AUDIO = R.layout.item_chat_audio_receive;
    private static final int MSG_SEND_VIDEO = R.layout.item_chat_video_send;
    private static final int MSG_RECEIVE_VIDEO = R.layout.item_chat_video_receive;
    private static final int MSG_SEND_STICKER = R.layout.item_chat_sticker_send;
    private static final int MSG_RECEIVE_STICKER = R.layout.item_chat_sticker_receive;
    private static final int MSG_NOTIFICATION = R.layout.item_chat_notification;
    private static final int MSG_UNKNOW = R.layout.item_chat_no_support_msg_type;

    private ChatType chatType;

    public ChatAdapter() {
        super(null);
    }

    public void setChatType(ChatType type) {
        this.chatType = type;
        notifyDataSetChanged();
    }

    public void setAdapterData(List<ImResponse> list) {
        setNewData(list);
    }

    public void addAdapterData(ImResponse response) {
        if (response instanceof ChatResponse) {
            if (ImHelper.isSelf(((ChatResponse) response).data.from)) { //判断是否是自己发的消息 且已经接收到
                if (response.isDirect) { //去除群组再次转发的
                    addData(response);
                }
            } else {
                addData(response);
            }
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        ImResponse response = mData.get(position);
        if (response instanceof ImResponse) {
            if (response instanceof ChatResponse) {
                ChatResponse chatResponse = (ChatResponse) response;
                boolean isSelf = ImHelper.isSelf(chatResponse.data.from);
                switch (MsgType.valueOf(chatResponse.data.msgType)) {
                    case MSG_TYPE_TEXT:
                        return isSelf ? MSG_SEND_TEXT : MSG_RECEIVE_TEXT;
                    case MSG_TYPE_IMG:
                        return isSelf ? MSG_SEND_IMAGE : MSG_RECEIVE_IMAGE;
                    case MSG_TYPE_VOICE:
                        return isSelf ? MSG_SEND_AUDIO : MSG_RECEIVE_AUDIO;
                    case MSG_TYPE_VIDEO:
                        return isSelf ? MSG_SEND_VIDEO : MSG_RECEIVE_VIDEO;
                }
            }
            return MSG_UNKNOW;
        }
        return super.getDefItemViewType(position);
    }

    @Override
    protected BaseRecyclerHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case MSG_RECEIVE_TEXT:
            case MSG_SEND_TEXT:
            case MSG_SEND_IMAGE:
            case MSG_RECEIVE_IMAGE:
            case MSG_SEND_AUDIO:
            case MSG_RECEIVE_AUDIO:
            case MSG_SEND_VIDEO:
            case MSG_RECEIVE_VIDEO:
            case MSG_UNKNOW:
                return createBaseViewHolder(getItemView(viewType, parent));
        }
        return super.onCreateDefViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case MSG_SEND_TEXT:
                convertText(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), true);
                break;
            case MSG_RECEIVE_TEXT:
                convertText(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), false);
                break;
            case MSG_SEND_IMAGE:
                convertImage(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), true);
                break;
            case MSG_RECEIVE_IMAGE:
                convertImage(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), false);
                break;
            case MSG_SEND_AUDIO:
                convertAudio(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), true);
                break;
            case MSG_RECEIVE_AUDIO:
                convertAudio(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), false);
                break;
            case MSG_SEND_VIDEO:
                convertVideo(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), true);
                break;
            case MSG_RECEIVE_VIDEO:
                convertVideo(holder, (ChatResponse) getItem(position - getHeaderLayoutCount()), false);
                break;
            case MSG_UNKNOW:
                convertUnknow(holder, getItem(position - getHeaderLayoutCount()));
                break;

        }
        super.onBindViewHolder(holder, position);
    }

    @Override
    protected void convert(BaseRecyclerHolder helper, ImResponse item) {

    }

    protected void convertText(BaseRecyclerHolder helper, ChatResponse item, boolean isSelf) {
        setTimeStatus(helper, item);
        helper.setText(R.id.tvName, item.data.from);
        setAvatar(helper, item, isSelf);
        MoonUtils.identifyFaceExpression(mContext, helper.getView(R.id.tvText), item.data.content, ImageSpan.ALIGN_BOTTOM);

    }

    protected void convertImage(BaseRecyclerHolder helper, ChatResponse item, boolean isSelf) {
        setTimeStatus(helper, item);
        setAvatar(helper, item, isSelf);
        helper.setText(R.id.tvName, item.data.from);
        String url = item.data.content;
        if (url.startsWith("http") && !url.startsWith("https") && !url.startsWith("http://")) {
            url = "http://" + url.substring(6, url.length());
        }
        ((ImageView)helper.getView(R.id.bivPic)).setAdjustViewBounds(true);
        Glide.with(mContext).load(url).apply(new RequestOptions().centerCrop().error(R.drawable.placeholder_square_failed).override(UIUtils.dip2px(80), UIUtils.dip2px(150))).into((ImageView) helper.getView(R.id.bivPic));
    }

    protected void convertAudio(BaseRecyclerHolder helper, ChatResponse item, boolean isSelf) {
        setTimeStatus(helper, item);
        setAvatar(helper, item, isSelf);
        helper.setText(R.id.tvName, item.data.from);
        helper.setText(R.id.tvDuration, String.format("%1$s%2$s", JSON.parseObject(item.data.content, VoiceBean.class).voiceRecordingSeconds, "''"));

    }

    protected void convertVideo(BaseRecyclerHolder helper, ChatResponse item, boolean isSelf) {
        setTimeStatus(helper, item);
        setAvatar(helper, item, isSelf);
        helper.setText(R.id.tvName, item.data.from);
    }

    protected void convertUnknow(BaseRecyclerHolder helper, ImResponse item) {

    }

    private void setTimeStatus(BaseRecyclerHolder helper, ChatResponse response) {
        if (isShowTime(response)) {
            helper.setGone(R.id.tvTime, true);
            helper.setText(R.id.tvTime, ImHelper.formatTime(response.data.createTime));
        } else {
            helper.setGone(R.id.tvTime, false);
        }
    }

    /**
     * 设置头像
     *
     * @param helper
     * @param response
     * @param isSelf
     */
    private void setAvatar(BaseRecyclerHolder helper, ChatResponse response, boolean isSelf) {
        if (isSelf) {
            helper.setImageManager(mContext, R.id.ivAvatar, ImHelper.getSelfAvatar(), R.drawable.placeholder_header);
        }
    }

    /**
     * 显示时间
     *
     * @param response
     * @return
     */
    private boolean isShowTime(ChatResponse response) {
        int i = mData.indexOf(response);
        if (i == 0) {
            return true;
        }
        ImResponse preResponse = getData().get(i - 1);
        if (preResponse instanceof ChatResponse) {
            return response.data.createTime - ((ChatResponse) preResponse).data.createTime > 5 * 60 * 1000;
        }
        return false;
    }
}
